import javax.swing.plaf.IconUIResource;
import java.util.concurrent.Semaphore;

/**
 * @author jcl_hqu
 * @date 2021/4/6 19:58
 */


public class ShareData {
    private int N=16;
    private int data = 0;
    Semaphore _full=new Semaphore(0);
    Semaphore _empty=new Semaphore(N);
    Semaphore _mutex=new Semaphore(1);
    MySemaphore full=new MySemaphore(0);
    MySemaphore empty=new MySemaphore(N);
    MySemaphore mutex=new MySemaphore(1);
    public void producer() throws InterruptedException {
        while (true){
//            System.out.println("生产者");
//            empty.P();
//            mutex.P();
            _empty.acquire();
            _mutex.acquire();
            data++;
            System.out.println("生产者生产，库存"+data);
            _mutex.release();
            _full.release();
//            mutex.V();
//            full.V();
        }
    }

    public void consumer() throws InterruptedException {
        while (true) {
//            System.out.println("消费者");
            _full.acquire();
            _mutex.acquire();
            data--;
            System.out.println("消费者消费，库存"+data);
            _mutex.release();
            _empty.release();
        }
    }
}